/**
 * Ingenius-Group S.A.S
 * Project: SAU
 * File: Guideline.java
 * Date: 17/11/2010
 */

package com.ingenius.sau.shared.model.guidelines;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.ingenius.sau.shared.model.core.BaseBean;

/**
 * Class <strong>Guideline</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@FetchGroup(name = "with_factors", members = { @Persistent(name = "factors") })
public class Guideline implements BaseBean {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	@Persistent(nullValue = NullValue.NONE)
	private String name;

	@Persistent(nullValue = NullValue.NONE)
	private String authorName;

	@Persistent(nullValue = NullValue.NONE)
	private String description;

	@Persistent(nullValue = NullValue.NONE)
	private Date date;

	@Persistent(mappedBy = "guideline")
	@Element(dependent = "true")
	private List<Factor> factors;

	// CONSTRUCTORS

	public Guideline() {
	}

	public Guideline(String id) {
		this.id = id;
	}

	// METHODS

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthorName() {
		return authorName;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<Factor> getFactors() {
		return (factors == null) ? factors = new ArrayList<Factor>() : factors;
	}

	public void setFactors(List<Factor> factors) {
		this.factors = factors;
	}

	public void addFactor(Factor factor) {
		getFactors().add(factor);
	}

	@SuppressWarnings("deprecation")
	@Override
	public String toString() {
		return name + " (" + (date.getYear() + 1900) + ")";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Guideline) {
			return ((Guideline) obj).getId().equals(id);
		}
		return false;
	}

	public Guideline toDTO() {
		if (this != null) {
			this.setFactors(null);
		}
		return this;
	}
}
